home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue26 / tiptrix / ClassPlu / ClassPlug.pas next >
Encoding:
Pascal/Delphi Source File  |  1997-09-17  |  1.5 KB  |  59 lines

  1. {ClassPlug -  FreeWare [OdiE -97]. }
  2.  
  3. unit ClassPlug;
  4.  
  5. interface
  6.  
  7. procedure ReplaceParentClass( DClass, OldParent, NewParent: TClass);
  8.  
  9. implementation
  10.  
  11. uses
  12.   Windows;
  13.  
  14. type
  15.   PP = ^Pointer; // Pointer of Pointer of Parent...
  16.  
  17. procedure ReplaceParentClass( DClass, OldParent, NewParent: TClass);
  18. var
  19.   a: ^Byte;
  20.   p1,p2: PP;
  21.   prot: Longint;
  22. begin
  23.   // Simple dummy check..
  24.   if ( NewParent = nil ) or ( DClass = nil ) then Exit;
  25.  
  26.   // Find the class Parent pointer of AClass
  27.   while ( DClass.ClassParent <> OldParent ) do begin
  28.     if DClass.ClassParent = nil then Exit;
  29.     if DClass.ClassParent = NewParent then Exit;
  30.     DClass := DClass.ClassParent;
  31.   end;
  32.  
  33.   a := Pointer(DClass);
  34.   inc(a,vmtParent);
  35.   p1 := Pointer(a);
  36.  
  37.   // Find the class Self pointer of NewParent, wich will be used to
  38.   // fill the place of the ParentClass Pointer...
  39.   // (this is also quite basic...)
  40.   a := Pointer(NewParent);
  41.   inc(a,vmtSelfPtr);
  42.   p2 := Pointer(a);
  43.  
  44.   // Big THANX to Cyril Jandia for the next 3 steps...
  45.   // Taken from the DMZ #24 class traps example...
  46.   // I had it all worked out, except for the VirtualProtect thingy..
  47.  
  48.   VirtualProtect(p1, SizeOf(Pointer), PAGE_READWRITE, @prot); // let's be brave
  49.   p1^ := p2; // let's be yet more brave
  50.   // time to be clean: not necessary but easy to do, then...
  51.   VirtualProtect(P1, SizeOf(Pointer), prot, @prot);
  52.  
  53. // use the next line to visualize the change...
  54. // TClass(PP(P1)^^).className
  55. end;
  56.  
  57. end.
  58.  
  59.